from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from answers.search_indexes import QuesIndex
from .models import QuesModel, QattrModel, CommentModel, CommentdetailModel, LabelModel, LabelUserModel, QuesLabelModel, \
    ReportcommentModel
from users.models import User


class UserSerilizer(serializers.ModelSerializer):
    """用户信息序列化器"""

    class Meta:
        model = User
        fields = ('username',)


class LableSerializer(serializers.ModelSerializer):
    """标签序列化器"""

    class Meta:
        model = LabelModel
        fields = '__all__'

class LableSerializerall(serializers.ModelSerializer):
    """标签序列化器"""

    class Meta:
        model = LabelModel
        fields = ('id','label_name','label_attennum')



class AnswersdetailSerializer(serializers.ModelSerializer):
    """问题详情序列化器"""
    user = UserSerilizer()
    label = LableSerializer()

    class Meta:
        model = QuesModel
        fields = '__all__'


class AnswersatrrSerializer(serializers.ModelSerializer):
    """问题属性序列化器"""
    ques = AnswersdetailSerializer()
    answer = UserSerilizer()
    class Meta:
        model = QattrModel
        exclude = ('id',)


class CdetailSerializer(serializers.ModelSerializer):
    """评论详情"""

    class Meta:
        model = CommentdetailModel
        fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):
    """评论序列化器"""
    # ques = AnswersdetailSerializer()
    user = UserSerilizer()
    content = CdetailSerializer()

    class Meta:
        model = CommentModel
        fields = '__all__'


class QuestCommentSerializer(serializers.ModelSerializer):
    commentmodel_set = CommentSerializer(many=True)
    user = UserSerilizer()
    label = LableSerializer()

    class Meta:
        model = QuesModel
        fields = '__all__'


class LableUserSerializer(serializers.ModelSerializer):
    """用户关注标签"""
    class Meta:
        model = LabelUserModel
        fields = ('label_id',)


class LabelQuestSerializer(serializers.ModelSerializer):
    """标签与问题序列化器"""
    label = LableSerializerall()
    ques = AnswersdetailSerializer()

    class Meta:
        model = QuesLabelModel
        fields = '__all__'


class CommentUserlikeSerializer(serializers.ModelSerializer):
    ''' 用户评论操作序列化器'''

    class Meta:
        model = ReportcommentModel
        exclude= ('id',)


class AnswersIndexSerializer(HaystackSerializer):
    """
    问答搜索数据的序列化器
    """
    class Meta:
        index_classes = [QuesIndex]
        fields = ('text','title','id','label','content')